/************************************************************************/
/*Copyright (c) James Zhao 2007,2008
/*This file is part of AdminOverlay.
/*AdminOverlay is free software: you can redistribute it and/or modify
/*it under the terms of the GNU General Public License as published by
/*the Free Software Foundation, either version 3 of the License, or
/*(at your option) any later version.
/*
/*AdminOverlay is distributed in the hope that it will be useful,
/*but WITHOUT ANY WARRANTY; without even the implied warranty of
/*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/*GNU General Public License for more details.
/*
/*You should have received a copy of the GNU General Public License
/*along with Foobar.  If not, see <http://www.gnu.org/licenses/>.                                                                     */
/*************************************************************************/
#include "TabPanel.h"

TabPanel::TabPanel(int x, int y, int width, int height,int pTitleMargin,
				   int pTabWidth, int pTabGap, int pTabHeight) : 
	ListPanel(x,y,width,height), tabWidth(pTabWidth), tabGap(pTabGap), 
		titleMargin(pTitleMargin), tabHeight(pTabHeight)
{
	TopColor(0xF72E332B);
	BottomColor(0xF72E332B);
	Parent(NULL);
	lineBuffer = NULL;


	leftLine = new LineVisual(0xCC5F6C59);
	rightLine = new LineVisual(0xCC5F6C59);;

	lineBuffer = new LineBuffer(1);
	lineBuffer->addLine(leftLine);
	lineBuffer->addLine(rightLine);

	title = new TextVisual(("AdminOverlay"), 0xFFAAAAAA, 0,0,180,16, TextVisualType_Title);
	//textBuffer->addTextVisual(title);

	activeContentPanel = NULL;

	connect(this, SIGNAL(mouseDown(QPoint, bool)), 
		SLOT(onMouseDown(QPoint, bool)), Qt::DirectConnection);
	
}

TabPanel::~TabPanel(void)
{
	delete leftLine;
	delete rightLine;
	delete lineBuffer;

}

void TabPanel::layout( QPoint offset )
{
	Height(parent->Height());
	Width(parent->Width());
	X(0);
	Y(0);
	layoutSelf(offset);
	if(NeedUpdate())
	{
		title->X(X() + 15);
		title->Y(GlobalPos().y() + title->Height() / 4);
		int curX = titleMargin;
		//TODO:
		int size = itemsVector.size();
		for(int i = 0; i < size; i++)
		{
			curX += tabGap;
			TabItem *item = (TabItem*)(itemsVector[i]);
			item->Width(tabWidth);
			item->Height(tabHeight);
			item->X(curX);
			//itemsVector[i]->layout(xOffset + curX, yOffset + 2);
			item->layout(offset);
			//height of each item
			curX += tabWidth + tabGap;
		}
		background->Y(GlobalY() + tabHeight);
		background->Height(Height() - tabHeight);
		layoutLines();
		NeedUpdate(false);
		if(activeContentPanel != NULL)
		{
// 			activeContentPanel->GlobalPos(GlobalPos() + tabHeight);
			activeContentPanel->X(0);
			activeContentPanel->Y(tabHeight);
 			activeContentPanel->Width(Width());
 			activeContentPanel->Height(Height() - tabHeight);
			activeContentPanel->layout(offset);			
		}
	}
}

void TabPanel::onMouseDown(QPoint point, bool isPrimaryTarget)
{
	int size = itemsVector.size();
	for(int i = 0; i < size; i++)
	{
		ListItem *li = itemsVector[i];
		//RECT itemBounds = li->LocalBounds();
		if(li->LocalBounds().contains(point))
		{
			onSelectItem(i);
			break;
		}
	}	
	NeedUpdate(true);
}

void TabPanel::draw( LPDIRECT3DDEVICE9 device )
{
	background->draw(device);
	ListPanel::draw(device);
	lineBuffer->draw(device);
	title->draw(device);
	if(activeContentPanel != NULL)
		activeContentPanel->draw(device);
}
void TabPanel::layoutLines()
{

	//			    2-> ----------------- <-3
	//					|				| 
	//-------------------				-------------------
	//^0				^1				^4				  ^5
	//
	
	int selectedStartX = titleMargin + 
		(selectedIndex == 0 ? tabGap : tabGap + ((selectedIndex ) * (tabWidth + 2 * tabGap )));
	int selectedEndX = selectedStartX + tabWidth;

	int yTop = GlobalY();
	int yBottom = GlobalY() + tabHeight;

	int x0 = GlobalX(),	y0 = yBottom;
	int x1 = selectedStartX, y1 = yBottom;
	int x4 = selectedEndX, y4 = yBottom;
	int x5 = GlobalX() + Width(), y5 = yBottom;

	leftLine->Point1(x0, y0); leftLine->Point2(x1, y1);
	rightLine->Point1(x4, y4); rightLine->Point2(x5, y5);

	int currentX = titleMargin;
	for(int i = 0; i < itemsVector.size(); i++)
	{
		currentX += tabGap;

		LineVisual *vLeftLine = tabLines[i * 3];
		LineVisual *centerLine = tabLines[i * 3 + 1];
		LineVisual *vRightLine = tabLines[i * 3 + 2];

		vLeftLine->Point1(currentX, yBottom);
		vLeftLine->Point2(currentX, yTop);

		centerLine->Point1(currentX, yTop);	
		centerLine->Point2(currentX + tabWidth, yTop);

		vRightLine->Point1(currentX + tabWidth, yTop); 
		vRightLine->Point2(currentX + tabWidth, yBottom);

		currentX += tabGap + tabWidth;

	}


}

void TabPanel::onDeviceReset()
{
	ListPanel::onDeviceReset();
	lineBuffer->onDeviceReset();
	for(int i = 0; i < itemsVector.size(); i++)
	{
		itemsVector[i]->onDeviceReset();
	}
}
void TabPanel::addItem( TabItem *item )
{
	ListPanel::addItem((ListItem*)item);
	LineVisual *l1 = new LineVisual(0xCC5F6C59);
	LineVisual *l2 = new LineVisual(0xCC5F6C59);
	LineVisual *l3 = new LineVisual(0xCC5F6C59);

	tabLines.push_back(l1);
	tabLines.push_back(l2);
	tabLines.push_back(l3);

	lineBuffer->addLine(l1);
	lineBuffer->addLine(l2);
	lineBuffer->addLine(l3);
	
	NeedUpdate(true);
}

void TabPanel::onSelectItem( int index )
{
	//from listpanel
	if(index >= itemsVector.size() || index < 0)
		return;
	if(selectedIndex != -1 && itemsVector[selectedIndex] != NULL)
	{
		TabItem *activeItem = (TabItem*)itemsVector[selectedIndex];
		//deselect the active item
		activeItem->IsSelected(false);
		//remove the tab's content panel so it no longer recieves events
		children.removeAt(children.indexOf(activeContentPanel));
	}
	selectedIndex = index;
	itemsVector[selectedIndex]->IsSelected(true);

	if((itemsVector.size() > selectedIndex) && (itemsVector[selectedIndex] != NULL))
	{
		activeContentPanel = ((TabItem*)itemsVector[selectedIndex])->contentPanel;
		//for event model
		children.append(activeContentPanel);
	}
	else
	{
		activeContentPanel = NULL;
	}
	emit selectedItemChanged(index);
	NeedUpdate(true);
}

void TabPanel::NeedUpdate( bool val )
{
	ListPanel::NeedUpdate(val);
	if(activeContentPanel != NULL)
		activeContentPanel->NeedUpdate(val);
}